# -*- coding:utf-8 -*-
"""
Author：Administrator
Date:2023年02月23日
"""

import docx
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Pt

# -*- coding:utf-8 -*-
"""
Author：Administrator
Date:2023年02月23日
"""

import docx
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Pt,Cm

def createElementsWithAttr(elements,attrs):
	myElements=[]
	for element in elements:
		myElement=docx.oxml.shared.OxmlElement(element)
		for attr in attrs:
			myElement.set(docx.oxml.ns.qn(attr),attrs[attr])
		myElements.append(myElement)
	return myElements

myDocument = docx.Document('背诵名篇.docx')

myParagraphs=myDocument.paragraphs
for myParagraph in myParagraphs:
	myInnerSectPr=myParagraph._element.pPr.sectPr
	if myInnerSectPr is not None:
		myInnerPgBorders=docx.oxml.shared.OxmlElement('w:pgBorders')
		myInnerPgBorders.set(docx.oxml.ns.qn('w:offsetFrom'),'page')
		myInnerElements=['w:top','w:left','w:bottom','w:right']
		myInnerAttrs={'w:val':'doubles','w:sz':'4','w:space':'24','w:color':'ff0000'}

		myInnerElements=createElementsWithAttr(myInnerElements,myInnerAttrs)
		for myInnerElement in myInnerElements:
			myInnerPgBorders.append(myInnerElement)
		myInnerSectPr.append(myInnerPgBorders)
mySectPr=myDocument._element.body.sectPr
myPgBorders=docx.oxml.shared.OxmlElement('w:pgBorders')
myPgBorders.set(docx.oxml.ns.qn('w:offsetFrom'),'page')
myElemnets=['w:top','w:left','w:bottom','w:right']
myAttrs={'w:val':'double','w:sz':'4','w:space':'24','w:color':'ff0000'}
myElemnets=createElementsWithAttr(myElemnets,myAttrs)
for myElement in myElemnets:
	myPgBorders.append(myElement)
mySectPr.append(myPgBorders)


myDocument.save("02散文名篇.docx")

